#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <sw/redis++/redis++.h>
#include <chrono>
#include <set>
#include <thread>
#include "util.hpp"
using namespace std::chrono_literals;

void test1(sw::redis::Redis& redis)
{
    std::cout << "zadd 和 zrange" << std::endl;
    redis.flushall();
    redis.zadd("key", "吕布", 99.5);
    redis.zadd("key", {
        std::make_pair("赵云", 98),
        std::make_pair("典韦", 97)
    });
    std::vector<std::pair<std::string, double>> members = {
        std::make_pair("关羽", 95),
        std::make_pair("张飞", 93)
    };
    redis.zadd("key", members.begin(), members.end());
    std::vector<std::string> result;
    auto it1 = std::back_inserter(result);
    redis.zrange("key", 0, -1, it1);
    printContainer(result);
    std::vector<std::pair<std::string, double>> result_pair;
    auto it2 = std::back_inserter(result_pair);
    redis.zrange("key", 0, -1, it2);
    printContainerPair(result_pair);

}

void test2(sw::redis::Redis& redis)
{
    std::cout << "zcard" << std::endl;
    redis.flushall();
    redis.zadd("key", "吕布", 99.5);
    redis.zadd("key", {
        std::make_pair("赵云", 98),
        std::make_pair("典韦", 97)
    });
    std::vector<std::pair<std::string, double>> members = {
        std::make_pair("关羽", 95),
        std::make_pair("张飞", 93)
    };
    redis.zadd("key", members.begin(), members.end());
    long long result = redis.zcard("key");
    std::cout << "result: " << result <<std::endl;
    
}

void test3(sw::redis::Redis& redis)
{
    std::cout << "zrem" << std::endl;
    redis.flushall();
    redis.zadd("key", "吕布", 99.5);
    redis.zadd("key", {
        std::make_pair("赵云", 98),
        std::make_pair("典韦", 97)
    });
    std::vector<std::pair<std::string, double>> members = {
        std::make_pair("关羽", 95),
        std::make_pair("张飞", 93)
    };
    redis.zadd("key", members.begin(), members.end());

    long long result = redis.zcard("key");
    std::cout << "result: " << result <<std::endl;
    redis.zrem("key", "吕布");
    result = redis.zcard("key");
    std::cout << "result: " << result <<std::endl;
}

void test4(sw::redis::Redis& redis)
{
    std::cout << "zscore" << std::endl;
    redis.flushall();
    redis.zadd("key", "吕布", 99.5);
    redis.zadd("key", {
        std::make_pair("赵云", 98),
        std::make_pair("典韦", 97)
    });
    std::vector<std::pair<std::string, double>> members = {
        std::make_pair("关羽", 95),
        std::make_pair("张飞", 93)
    };
    redis.zadd("key", members.begin(), members.end());

    auto score = redis.zscore("key", "吕布");
    if(score){
        std::cout << "score: " << score.value()  <<std::endl;
    }
    else{
        std::cout << "score 无效" << std::endl;
    }
}

void test5(sw::redis::Redis& redis)
{
    std::cout << "zrank" << std::endl;
    redis.flushall();
    redis.zadd("key", "吕布", 99.5);
    redis.zadd("key", {
        std::make_pair("赵云", 98),
        std::make_pair("典韦", 97)
    });
    std::vector<std::pair<std::string, double>> members = {
        std::make_pair("关羽", 95),
        std::make_pair("张飞", 93)
    };
    redis.zadd("key", members.begin(), members.end());

    auto rank = redis.zrank("key", "吕布");
    if(rank){
        std::cout << "rank: " << rank.value()  <<std::endl;
    }
    else{
        std::cout << "rank 无效" << std::endl;
    }
}

int main()
{
    sw::redis::Redis redis("tcp://127.0.0.1:6379");
    // test1(redis);
    // test2(redis);
    // test3(redis);
    // test4(redis);
    test5(redis);
    return 0;
}